import com.adobe.serialization.json.JSON;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.ListEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;

private function onTrItemClick(event:ListEvent):void {
	if (trDatagrid.selectedIndex >= 0) {
		translationText.text = trDatagrid.selectedItem.@text;
		nameLabel.text = trDatagrid.selectedItem.@name;
		original.text = project.locale.(@code == masterLocale).entry.(@name == trDatagrid.selectedItem.@name)[0].@text;
		suggestion.text = "";
		translationText.enabled = true;
		translationText.setFocus();
	}
	else {
		translationText.enabled = false;
		clearAllInTrTab();
	}
}

private function onTranslationChange():void {
	if (trDatagrid.selectedIndex >= 0) {
		var n:Number = trDatagrid.selectedIndex;
		project.locale.(@code == transCombo.selectedItem.toString()).entry.(@name == trDatagrid.selectedItem.@name).@text = translationText.text;
		trDatagrid.selectedIndex = n;
		projectModified = true;
	}
}

private function onTrLocaleChange():void {
	clearAllInTrTab();
}

private function copySuggestion():void {
	translationText.text = suggestion.text;
	onTranslationChange();
}

private function initTrDatagrid():void {
	transCombo.selectedIndex = -1;
	transCombo.rowCount = 20;
}

private function onShow():void {
	var old:int = transCombo.selectedIndex;
	locales = new ArrayCollection();
	for each (var l:XML in project.locale.(@code != masterLocale)) {
		locales.addItem(l.@code.toString());
	}
	//transCombo. = project.locale.(@code != masterLocale)[0].@code;
	
	transCombo.selectedIndex = old;
	if (transCombo.selectedIndex != old) {
		transCombo.selectedIndex = -1;
	}
	//trDatagrid.setStyle("dataProvider", "{project.locale.(@code == transCombo.selectedItem.toString()).entry}");
	
	
	if (trDatagrid.selectedIndex < 0) {
		clearAllInTrTab();
	}
}

private function translate():void {
	var service:HTTPService = new HTTPService();
   	var source:String = masterLocale.substr(0, 2);
   	var dest:String = transCombo.value.toString().substr(0, 2);
    service.url = 'http://ajax.googleapis.com/ajax/services/language/translate';
    service.request.v = '1.0';
    service.request.langpair = source + "|" + dest;
    service.request.q = original.text;
    service.addEventListener(ResultEvent.RESULT, onServerResponse);
    service.addEventListener(FaultEvent.FAULT, onFaultResponse);
    service.send();
}

private function onServerResponse(event:ResultEvent):void {  
    try {   
        var json:Object = JSON.decode(event.result as String);
        var s:String = "<root><entry>" + json.responseData.translatedText + "</entry></root>";
        var x:XML = new XML(s);
        suggestion.text = x.entry[0].toString();
    } catch(ignored:Error) { 
        suggestion.text = "Error during JSON decoding!";
    }
}

private function onFaultResponse(event:FaultEvent):void {
    Alert.show(event.fault.message);
}

private function clearAllInTrTab():void {
	nameLabel.text = "";	
	suggestion.text = "";
	translationText.text = "";
	original.text = "";
}